package org.qpcr.community.standalone.web.response.dashboard;

import lombok.Getter;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Getter
public enum ExperimentDateEnums {
    //今日
    TODAY("今日", "today"),
    //近一周
    WEEK("近一周", "week"),
    //近一个月
    MONTH("近一个月", "month"),;
    private final String name;
    private final String value;

    ExperimentDateEnums(String name, String value) {
        this.name = name;
        this.value = value;
    }

    //传人value计算出日期区间
    public static Map<String,LocalDateTime> getDateRange(String value) {
        Map<String,LocalDateTime> dateRange = new HashMap<>();
        LocalDateTime now = LocalDateTime.now();

        // 处理value为null的情况，默认返回今日
        if (value == null) {
            LocalDateTime todayStart = now.toLocalDate().atStartOfDay();
            LocalDateTime todayEnd = todayStart.with(LocalTime.MAX);
            dateRange.put("start", todayStart);
            dateRange.put("end", todayEnd);
            return dateRange;
        }

        switch (value) {
            case "today":
                // 今日：从今天00:00:00到23:59:59
                LocalDateTime todayStart = now.toLocalDate().atStartOfDay();
                LocalDateTime todayEnd = todayStart.with(LocalTime.MAX);
                dateRange.put("start", todayStart);
                dateRange.put("end", todayEnd);
                break;
            case "week":
                // 近一周：从7天前的00:00:00到今天23:59:59
                LocalDateTime weekStart = now.minusDays(7).toLocalDate().atStartOfDay();
                LocalDateTime weekEnd = now.toLocalDate().atTime(LocalTime.MAX);
                dateRange.put("start", weekStart);
                dateRange.put("end", weekEnd);
                break;
            case "month":
                // 近一个月：从30天前的00:00:00到今天23:59:59
                LocalDateTime monthStart = now.minusDays(30).toLocalDate().atStartOfDay();
                LocalDateTime monthEnd = now.toLocalDate().atTime(LocalTime.MAX);
                dateRange.put("start", monthStart);
                dateRange.put("end", monthEnd);
                break;
            default:
                // 默认返回今日
                LocalDateTime defaultStart = now.toLocalDate().atStartOfDay();
                LocalDateTime defaultEnd = defaultStart.with(LocalTime.MAX);
                dateRange.put("start", defaultStart);
                dateRange.put("end", defaultEnd);
                break;
        }
        return dateRange;
    }

}
